package com.informatics.polymer.client.propertysearch;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
/**
 * Class to represent a panel for property search.
 * @author ed
 * @version 1.0
 */
public class PropertySearchPanel extends SectionStack {
	/**
	 * Create a PropertySearchPanel.
	 */
	public PropertySearchPanel(){
		setUpPanel();
	}
	
	private void setUpPanel(){
		this.setVisibilityMode(VisibilityMode.MULTIPLE);
		this.setWidth(1000);
		this.setHeight(500);
		this.setBorder("2px solid #458B00");
		SectionStackSection section1 = new SectionStackSection(
				"Property Search");
		section1.setExpanded(true);
		HTMLFlow welcome = new HTMLFlow();
		welcome.setOverflow(Overflow.AUTO);
		welcome.setPadding(10);
		String welcomeString = "Welcome to the <I>\"Property Search\"</I> panel, please see the instructions tab "
				+ "below for more details on how to search for repeat units.";
		welcome.setContents(welcomeString);
		section1.addItem(welcome);
		this.addSection(section1);
		this.setStyleName("sectionHeaderclosed");
		this.setBackgroundColor("#F0F0F0");
		// Search section.
		SectionStackSection searchSection = new SectionStackSection("Search");
		final PropertyTree propertyTree = new PropertyTree();
	    HStack propertyHLayout = propertyTree.createPropertyTree();
		//Add button on click gets contents of selected tree grid
		propertyHLayout.draw();
		searchSection.addItem(propertyHLayout);
		this.addSection(searchSection);
		//View section
		SectionStackSection viewSection = new SectionStackSection("View");
		final ListGrid browsingGrid = setUpListGrid4Browsing();
		HLayout browseLayout = new HLayout();
		browseLayout.addMember(browsingGrid);
		viewSection.addItem(browsingGrid);
		this.addSection(viewSection);
		// Add instructions section.
		SectionStackSection instructionSection = new SectionStackSection(
				"Instructions");
		instructionSection.setExpanded(false);
		HTMLFlow browseRepeatUnitInstructions = new HTMLFlow();
		browseRepeatUnitInstructions.setOverflow(Overflow.AUTO);
		browseRepeatUnitInstructions.setPadding(10);
		String instructions = "1. <b>Using the Search Panel.</b><br></br> Use the search tab to browse the property tree for a property you wish to search for repeat units.<br></br>"
			+"Left click on the property, fill in the minimum and maximum value for each property in the grid provided, then click the submit button. Your results will appear in the \"<I>Search Results</I>\"tab.<br></br>"	
			+ "2.<b>Delete Panel</b><br></br> If you are not happy with your selection or choose to view another panel you can click on the <I>\"Delete Panel\"</I> and press the delete panel button"
				+ " this will delete the browse repository panel, so you can start over again.<br></br>";
		browseRepeatUnitInstructions.setContents(instructions);
		instructionSection.addItem(browseRepeatUnitInstructions);
		this.addSection(instructionSection);
		// Delete panel section
		SectionStackSection deletePanelSection = new SectionStackSection(
				"Delete Panel");
		deletePanelSection.setExpanded(false);
		Button deletePanelButton = new Button("Delete Panel!");
		deletePanelButton.setSize("100", "30");
		deletePanelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		deletePanelSection.addItem(deletePanelButton);
		this.addSection(deletePanelSection);
	}
	/**
	 * Constructs a listgrid to browse the repository.
	 * 
	 * @return ListGrid representation of all the data stored in the database.
	 */
	private ListGrid setUpListGrid4Browsing() {
		ListGrid fullRepositoryGrid = new ListGrid();
		fullRepositoryGrid.setSelectionType(SelectionStyle.SIMPLE);
		fullRepositoryGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		fullRepositoryGrid.setPixelSize(500, 300);
		ListGridField idValue = new ListGridField("Id", "ID", 100);
		ListGridField structure = new ListGridField("URL",
				"Structure", 400);
		structure.setType(ListGridFieldType.IMAGE);
		structure.setImageHeight(200);
		structure.setImageWidth(400);
		ListGridField alogp = new ListGridField("ALOGP", "ALOGP", 100);
		ListGridField alogP2 = new ListGridField("AlogP2", "ALOGP2", 100);
		ListGridField amr = new ListGridField("AMR", "AMR", 100);
		ListGridField aPol = new ListGridField("APol", "APol", 100);
		ListGridField aromaticAtomCount = new ListGridField(
				"AromaticAtomCount", "AAC", 100);
		ListGridField aromaticBondCount = new ListGridField(
				"AromaticBondCount", "ABC", 100);
		ListGridField atomCount = new ListGridField("AtomCount", "AtomCount");
		ListGridField bondCount = new ListGridField("BondCount", "BondCount");
		ListGridField fragmentComplexity = new ListGridField(
				"FragmentComplexity", "FC", 100);
		ListGridField hbaCount = new ListGridField("HBACount", "HBA", 100);
		ListGridField hbdCount = new ListGridField("HBDCount", "HBD", 100);
		ListGridField kappa1 = new ListGridField("Kappa1", "Kappa1", 100);
		ListGridField kappa2 = new ListGridField("Kappa2", "Kappa2", 100);
		ListGridField kappa3 = new ListGridField("Kappa3", "Kappa3", 100);
		ListGridField ruleOf5 = new ListGridField("LipinskiRuleOf5Failures",
				"Lipinski", 100);
		ListGridField tpsa = new ListGridField("TPSA", "TPSA", 100);
		DataSource data = new DataSource(
				"http://localhost:8080/PropertySearchDump.xml");
		data.setRecordXPath("/RepeatUnits/RepeatUnit");		
		data.setDataFormat(DSDataFormat.XML);
		fullRepositoryGrid.setFields(idValue, structure, alogp, alogP2, amr,
				aPol, aromaticAtomCount, aromaticBondCount, atomCount,
				bondCount, fragmentComplexity, hbaCount, hbdCount, kappa1,
				kappa2, kappa3, ruleOf5, tpsa);
		fullRepositoryGrid.setDataSource(data);
		fullRepositoryGrid.setShowAllRecords(true);
		fullRepositoryGrid.fetchData();
		return fullRepositoryGrid;
	}

	
}
